package javaee.synchronize;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author tongchen
 * @create 2023-04-11 23:14
 */
public class SynchronizeTest {
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        //使用futureTask和callable接口实现线程的创建
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                int a = 10;
                int b = 20;
                return a + b;
            }
        };
        //创建futureTask
        FutureTask<Integer> integerFutureTask = new FutureTask<>(callable);
        //如果使用futureTask不创建线程执行方法，get方法会一直阻塞
        Thread thread1 = new Thread(integerFutureTask);
        thread1.start();
        System.out.println(integerFutureTask.get()+"这是get方法创建的线程生成的返回值....");
        //使用runnable接口创建线程
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println("我是使用runnable接口创建的线程");
            }
        };
        Thread thread = new Thread(runnable);
        thread.start();

        //在主线程中创建任务并执行
        Thread t1 =new Thread(()->{
            increase();
        });
        t1.start();
        increase();
        //创建线程2

          Thread t3 =new Thread(()->{
                     increase();
                  });
         // t3.start();
        MyThread myThread = new MyThread();
        myThread.start();
        t1.join();
        //t3.join();
        System.out.println(count);


    }
    public static int count=0;
    public static void increase(){

        for (int i=0;i<50000;++i){
            count++;
        }
    }
}
class MyThread extends Thread{
    @Override
    public void run() {
        System.out.println("myThreadStart.......");
    }
}
